

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 *
 * @author Circle
 */
public class ManagerExecutionClient implements IManager<ExecutionClient> {

    public final String FileName = "ExecutionClient.txt";
    public ArrayList<ExecutionClient> lstObject;
    public int length;

    public ManagerExecutionClient(){
        lstObject = new ArrayList<>();
        length = 0;
        File f = new File(FileName);
        try {
            if (!f.exists()) {
                f.createNewFile();
                Database();
            } else {
                ObjectInputStream OIStream = new ObjectInputStream(new FileInputStream(FileName));
                while (true) {
                    ExecutionClient temp = (ExecutionClient) OIStream.readObject();
                    lstObject.add(temp);    
                    length++;                
                }
            }
        } catch (IOException | ClassNotFoundException ex) {
            System.out.printf("Can't open data ExecutionClient");
        }
    }

    @Override
    public ExecutionClient GetById(int id) {
        for (int i = 0; i < length; i++) {
            if (lstObject.get(i).getId() == id) {
                return lstObject.get(i);
            }
        }
        return null;
    }

    @Override
    public ArrayList<ExecutionClient> GetList() {
        return lstObject;
    }

    @Override
    public void AddToList(ExecutionClient obj) {
        lstObject.add(obj);
        length++;
    }

    @Override
    public boolean DeleteById(int id) {
        ExecutionClient temp = GetById(id);
        if (temp.getId() != -1) {
            lstObject.remove(id);
            length--;
            System.out.println("Delete success");
            return true;
        } else {
            System.out.println("Can't find id in database");
            return false;
        }
    }

    @Override
    public boolean ChangeInfo(int index, ExecutionClient obj) {
        try {
            lstObject.add(index, obj);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public void ShowInfoOfList() {
        if (length < 0) {
            System.out.println("Blank Data of Advisory Client");
        } else {
            for (int i = 0; i < length; i++) {
                System.out.print(i + 1);
                lstObject.get(i).toString();
            }
        }
    }

    @Override
    public boolean SaveDataToFile() {
        try {
            try (ObjectOutputStream OOStream = new ObjectOutputStream(new FileOutputStream(FileName))) {
                for (int i = 0; i < length; i++) {
                    OOStream.writeObject(lstObject.get(i));
                }
                OOStream.flush();
                OOStream.close();
            }
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
    
    public void Database()
    {
        ExecutionClient vt1 = new ExecutionClient(1, "executionclient", "password", "abc",50);
        AddToList(vt1);
        SaveDataToFile();
    }  
    
    ExecutionClient SignIn(String username, String password)
    {
        for (int i = 0; i < length; i++)
        {
            if ((username.equals(lstObject.get(i).getName())) && (password.equals(lstObject.get(i).getPassword())))
               return lstObject.get(i);
        }      
        return null;
    } 
    
    boolean ChangePassword(ExecutionClient obj, String oldPassword, String newPassword, String confirmPassword)
    {
        if (!oldPassword.equals(obj.getPassword()) || !newPassword.equals(confirmPassword))
            return false;
        for (int i = 0; i < length; i++)
        {
            if ((obj.getName().equals(lstObject.get(i).getName())) && (obj.getPassword().equals(lstObject.get(i).getPassword())))
            {
              lstObject.get(i).setPassword(newPassword);
              SaveDataToFile();
              return true;
            }
        }
        return false;
    }
    
    boolean ChangeEmail(ExecutionClient obj, String newEmail)
    {
        for (int i = 0; i < length; i++)
        {
            if ((obj.getName().equals(lstObject.get(i).getName())) && (obj.getPassword().equals(lstObject.get(i).getPassword())))
            {
              lstObject.get(i).setEmail(newEmail);
              SaveDataToFile();
              return true;
            }
        }
        return false;

    }
    
    boolean DepositOrWithdrawCash(ExecutionClient obj, int choice, float balance)
    {
        for (int i = 0; i < length; i++)
        {
            if ((obj.getName().equals(lstObject.get(i).getName())) && (obj.getPassword().equals(lstObject.get(i).getPassword())))
            {
               if (choice == 1)
               {
                  lstObject.get(i).depositCash(balance);
                  SaveDataToFile();
                  return true;
               }
               else
               {
                  if (lstObject.get(i).getCash() > balance)
                  {
                     lstObject.get(i).withdrawCash(balance);
                     SaveDataToFile();
                     return true;
                  }
                  else
                     return false;
               }
            }
        }
        return false;

    }
    
    boolean BuyShares(ExecutionClient obj, Stock stock, int nShares)
    {
        for (int i = 0; i < length; i++)
        {
            if ((obj.getName().equals(lstObject.get(i).getName())) && (obj.getPassword().equals(lstObject.get(i).getPassword())))
            {
               float balance = nShares * stock.getPrice();
               if (obj.getCash() > balance)
               {
                  lstObject.get(i).withdrawCash(balance);
                  return true;
               }
               return false;
            }
        }
        return false;
    }

   boolean SellShares(ExecutionClient obj, Stock stock, int nShares)
    {
        for (int i = 0; i < length; i++)
        {
            if ((obj.getName().equals(lstObject.get(i).getName())) && (obj.getPassword().equals(lstObject.get(i).getPassword())))
            {
               float balance = nShares * stock.getPrice();
               if (obj.getCash() > balance)
               {
                  lstObject.get(i).depositCash(balance);
                  return true;
               }
               return false;
            }
        }
        return false;
    }

}

